/**
 * customer difined validation rules
 * and extension for jquery validator
 * author: alva yican@csair.com alva.yi@gmail.com
 */
/**
 * extend jQuery validator
 * NOTE: for the following stuff, may be there is some good options
 * just don't rely on this extensions
 */
$.extend($.validator.prototype, {
	removeError: function(element, parent){
		//remove the generated error for a programtic eliminate error message
		$(this.settings.errorElement + '[for=' + element + ']').remove();
	},
	removeErrors: function(element, parent) {
		//try match the prefix
		var matched = $(this.settings.errorElement + '[for^=' + element + ']');
		if (matched) {
			matched.remove();
		} else {
			//then match contains
			matched = $(this.settings.errorElement + '[for*=' + element + ']');
			matched.remove();
		}
	}
});
/**
 * remove all the error message of the jqueryValidator
 * use this if you don't want to show the error message
 */
function clearErrorMessages() {
	//clear the message
	for (var msg in jQuery.validator.messages) {
		jQuery.validator.messages[msg] = '';
	}
}
/**
 * check the input is Chinese or not 
 * TODO limited to simple chinese range - find the unicode range
 */
jQuery.validator.addMethod("chinese", function(value, element) {
	return this.optional(element) || /^[\u4e00-\u9fa5]+$/.test(value);
}, "请输入中文");
/**
 * check chinese name, allow Pinyin as parts of Chinesename
 */
jQuery.validator.addMethod("cname", function(value, element){
	return this.optional(element) || /^[A-Z\u4e00-\u9fa5]+$/.test(value);
}, "请输入中文或拼音");
/**
 * check if the input is capitalize letters
 */
jQuery.validator.addMethod("letter", function(value, element) {
	return this.optional(element) || /^[A-Z]+$/.test(value);
}, "请输入字母");
/**
 * check if the input is letters
 */
jQuery.validator.addMethod("alpha", function(value, element) {
	return this.optional(element) || /^[A-Za-z]+$/.test(value);
}, "请输入字母");
/**
 * test the valid date
 */
jQuery.validator.addMethod(
	"validDate",
	function(value, element) {
		return this.optional(element) || checkDate(value);
	}, 
	"请输入一个合法的日期"
);
/**
 * test the valid birthDay
 * a valid birthday should at less then today!
 */
jQuery.validator.addMethod(
	"birthday",
	function(value, element) {
		//check the basic date
		return this.optional(element) || checkDate(value, new Date());
	},
	"请输入一个合法的生日"
);
/**
 * test the valid date
 * a valid past date should at less then today!
 */
jQuery.validator.addMethod(
	"pastDate",
	function(value, element) {
		//check the basic date
		return this.optional(element) || checkDate(value, new Date());
	},
	"请输入不早于今天的合法日期"
);
/**
 * test the valid id card number
 * (1) 15 digits
 * (2) 17 digits followed by a verify code
 */
jQuery.validator.addMethod(
	"idCard",
	function(value, element) {
		//check the idcard
		return this.optional(element) || checkIdCard(value);
	},
	"请输入合法的身份证"
);
/**
 * test  letters and digits greater then six is acceptable
 */
jQuery.validator.addMethod(
	"aorno", //alpha or number
	function(value, element) {
		return this.optional(element) || /^[A-Z0-9]+$/.test(value);
	},
	"系统只保存证件号中的英文字母及数字，请核查"
);

jQuery.validator.addMethod(
		"passport", //alpha or number
		function(value, element) {
			if(value.length<6){
				return false;
			}
			return this.optional(element) || /^(([0-9][0-9a-zA-Z]*[a-zA-Z][0-9a-zA-Z]*)|([a-zA-Z][0-9a-zA-Z]*[0-9][0-9a-zA-Z]*))$/.test(value);
		},
		"请输入至少6位(字母和数字)组成的护照"
);

jQuery.validator.addMethod(
	"zipcode",
	function(value, element) {
		return this.optional(element) || /^\d{6}$/.test(value);
	},
	"请正确输入邮编"
);
jQuery.validator.addMethod(
	"mobile",
	function(value, element) {
		return this.optional(element) || /^\d{11}$/.test(value);
	},
	"手机号码不正确"
);
/**
 * added a general regular express validator method, e.g.
 * $('#TextBox').rules('add', {regex: "/^reg_exp$/"});
 * //or you can specify this when initialize
 * $('#form1').validate({
 * 		rules: {
 * 			TextBox: {
 * 				required,
 * 				regex: "/^reg_exp$/"
 * 			},
 * 		},
 * });
 * TODO add a custom tag to simplify this approach
 */
jQuery.validator.addMethod(
	"regex",
	function(value, element, param) {
		//check if the valud match the regex
		return this.optional(element) || value.match(new RegExp(param));
	},
	"输入不符合格式要求"
);
////////////////////////////////////////////////validator related function
/**
 * check a valid date string
 */
function checkDate(value, limit) {
	var check = false;
	var re = /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/
	if( re.test(value)){
		//convert to a uniform format
		if (value.indexOf('-') != -1) {
			value = value.replace(/-/g, '/');
		}
		var adata = value.split('/');
		var aaaa = parseInt(adata[0],10);
		var mm = parseInt(adata[1],10);
		var gg = parseInt(adata[2],10);
		var xdata = new Date(aaaa,mm-1,gg);
		if ( ( xdata.getFullYear() == aaaa ) && ( xdata.getMonth () == mm - 1 ) && ( xdata.getDate() == gg ) ) {
			check = true;
			//check the limit
			if (limit != null && xdata > limit) {
				check = false;
			}
		} else
			check = false;
	} else {
		check = false;
	}
	return check;
}
/**
 * check if the id card number valid or not
 * @param - value, id card number
 */
function checkIdCard(value) {
	//step1: 15 or 18 digits or with a X at last of the string
	if (/^\d{15}$/.test(value)) {
		return true;
	} 
	//18 bits
	if (/^\d{17}[0-9X]$/.test(value)) {
		//check the verify code
		//factors define
		var wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
		//verify code array
		var vi = [ "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
		var sum = 0;
		for (var i = 0; i < 17; i++) {
			sum += wi[i] * parseInt(value.charAt(i));
		}
		var mod = sum % 11;
		if (vi[mod] == value.charAt(17)) {
			return true;
		} else {
			return false;
		}
	}
	//finally
	return false;
}
